<?php


class DataSupplyProductItem extends Eloquent
{
	protected $connection = 'mysql_data';
    protected $table = "supply_product_item";



    /**
     * 根据车型id和耗材分类id 获取 一条默认的耗材信息【cms的耗材信息 也既时anhui库中的耗材信息】
     * @param int $car_model_id     车型id
     * @param int $supply_subject   耗材分类id  [anhui库中]
     * @return array | null
     */
      public static function getDefault($car_model_id,$supply_subject){
        // $arr= DB::select("SELECT spi.number,spi.max_number,si.*,ss.`name`,sb.brand_name,ssb.name as ssb_name FROM `supply_product_item` spi
        //     LEFT JOIN supply_item si on si.id=spi.supply_item_id
        //     LEFT JOIN supply_brand sb on si.brand_id=sb.id
        //     LEFT JOIN supply_series ss on ss.id=si.series_id
        //     LEFT JOIN supply_subject ssb on ssb.id=si.`subject`
        //     where 
        //     and spi.car_model_id=? and `subject`=? order by spi.isdefualt desc ",array($car_model_id,$supply_subject));

      	// $csi=DataCarModel::find($car_model_id); 
       //  $tuijian=SupplyRecommend::getRecommendByCarModel($csi);

        $dmcc=DataCarMaintenanceClassificationConfig::where("car_model_id",$car_model_id)->where("supply_classification_id",$supply_subject)->where("status",1)->first();
        // DB::select("select si.id 
        // 		from 	  anhui_data.supply_item as si
        // 		LEFT JOIN anhui_data.supply_brand sb on si.brand_id=sb.id
        //     	LEFT JOIN anhui_data.supply_series ss on ss.id=si.series_id
        //         Left Join anhui_data.supply_product_item spi on spi.supply_item_id=si.id
        //         where spi.car_model_id=? and spi.status=1 and si.status=1 and  sb.status=1 and ss.status=1 and si.supply_classification_id=?");
        $temp_dsi=SupplyItem::
        	leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")->
        	leftJoin("anhui_data.supply_series","supply_series.id","=","supply_item.series_id")->
        	leftJoin("anhui_data.supply_product_item","supply_product_item.supply_item_id","=","supply_item.erp_id")->
        	where("supply_product_item.car_model_id",$car_model_id)->
        	where("supply_product_item.status",1)->
        	where("supply_item.status",1)->
        	// where("supply_series.status",1)->
        	where("supply_brand.status",1)->
        	where("supply_item.supply_classification_id",$supply_subject);
        // if(sizeof($tuijian)>0){ //如果说改成行存在推荐
        // 	$ddi=clone $temp_dsi;
        // 	$ddi=$ddi->whereIn("supply_item.id",$tuijian);
        // 	$ddi=$ddi->select("supply_item.id","supply_item.desc")->orderBy("supply_item.id","asc")->first();
        // }   
        // if(!isset($ddi)||is_null($ddi)){//如果不存在 推荐或者 推荐的为空 按照是适配 查找
        	$ddi=$temp_dsi->select("supply_item.id","supply_item.erp_id","supply_item.small_desc","supply_product_item.number","supply_product_item.max_number")->orderBy("supply_product_item.isdefualt","desc")->first();
        // }          
        if($ddi){
        	$item_info=SupplyItem::getCacheItemInfo($ddi->id);
            $dsii=DataSupplyItemIcon::where("supply_item_id",$ddi->erp_id)->where("status",1)->first();
        	$ddi->model=$item_info->model;
        	$ddi->brand_id=$item_info->brand_id;
        	$ddi->brand_name=$item_info->brand_name;
        	$ddi->series_id=$item_info->series_id;
        	$ddi->series_name=$item_info->series_name;
        	$ddi->subject=$item_info->supply_classification_id;
        	$ddi->subject_name=$item_info->subject_name;
        	// $ddi->number=isset($dmcc)&&$dmcc->number!=null?$dmcc->number:$ddi->number;
        	// $ddi->max_number=isset($dmcc)&&$dmcc->number!=null?$dmcc->number:$ddi->max_number;
        	$ddi->icon=isset($dsii)?$dsii->icon:"";
        	$ddi->small_desc=$ddi->small_desc;
        	$ddi->amount=$item_info->amount?$item_info->amount:0;
        	$ddi->unit=$item_info->unit?$item_info->unit:"";
        	$ddi->name=$item_info->name;
        	return $ddi;
        }else{
        	return null;
        }

        // $supply_arr=array(
        //     "id"=>$array->id,
        //     "model"=>$array->model,
        //     "brand_id"=>$array->brand_id,
        //     "brand_name"=>$array->brand_name,
        //     "series_id"=>$array->series_id,
        //     "series_name"=>$array->name,
        //     "subject"=>$array->subject,
        //     "subject_name"=>$array->ssb_name,
        //     "number"=>$array->number,
        //     "max_number"=>$array->max_number,
        //     "icon"=>$array->icon,
        //     "small_desc"=>$array->small_desc,
        //     "desc"=>$array->desc,
        //     "amount"=>$array->amount,
        //     "unit"=>$array->sale_unit,
        // );
        // return isset($arr[0])?$arr[0]:null;
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-11-01 11:47:06
     * 根据车型id和耗材分类id 获取 一条默认的耗材信息【cms的耗材信息 也既时anhui库中的耗材信息】 C端3.0使用
     * @param int $car_model_id     车型id
     * @param int $supply_subject   耗材分类id  [anhui库中]
     * @return array | null
     */
    public static function getDefaultC($car_model_id,$supply_subject){
        $dmcc=DataCarMaintenanceClassificationConfig::where("car_model_id",$car_model_id)->where("supply_classification_id",$supply_subject)->where("status",1)->first();
        $temp_dsi=SupplyItem::
        leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")->
        leftJoin("anhui_data.supply_series","supply_series.id","=","supply_item.series_id")->
        leftJoin("anhui_data.supply_product_item","supply_product_item.supply_item_id","=","supply_item.erp_id")->
        where("supply_product_item.car_model_id",$car_model_id)->
        where("supply_product_item.status",1)->
        where("supply_item.status",1)->
        where("supply_brand.status",1)->
        where("supply_item.supply_classification_id",$supply_subject);
        // if(!isset($ddi)||is_null($ddi)){//如果不存在 推荐或者 推荐的为空 按照是适配 查找
        $ddi=$temp_dsi->select("supply_item.id","supply_item.erp_id","supply_item.small_desc","supply_product_item.number","supply_product_item.max_number")->orderBy("supply_product_item.isdefualt","desc")->get();
        // }
        if(!is_null($ddi)){
            foreach($ddi as $key => $val){
                $item_info=SupplyItem::getCacheItemInfo($val->id);
                $dsii=DataSupplyItemIcon::where("supply_item_id",$val->erp_id)->where("status",1)->first();
                $ddi[$key]->model=$item_info->model;
                $ddi[$key]->brand_id=$item_info->brand_id;
                $ddi[$key]->brand_name=$item_info->brand_name;
                $ddi[$key]->series_id=$item_info->series_id;
                $ddi[$key]->series_name=$item_info->series_name;
                $ddi[$key]->subject=$item_info->supply_classification_id;
                $ddi[$key]->subject_name=$item_info->subject_name;
                $ddi[$key]->icon=isset($dsii)?$dsii->icon:"";
                $ddi[$key]->small_desc=$val->small_desc;
                $ddi[$key]->amount=$item_info->amount?$item_info->amount:0;
                $ddi[$key]->unit=$item_info->unit?$item_info->unit:"";
                $ddi[$key]->name=$item_info->name;
                $ddi[$key]->desc=$item_info->desc;
            }
            return $ddi;
        }else{
            return null;
        }
    }

}


